﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
using System.Xml;
using Lswweb.Model;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
using Lswweb.Common;

namespace Lswweb.Domain
{
    public class Maps
    {
        #region 成员变量及构造方法

        private Maps(){}

        #endregion

        #region 静态公共方法

        private static Maps _maps;
        public static Maps Content
        {
            get
            {
                if (_maps == null)
                    _maps = new Maps();
                return _maps;
            }
        }

        #endregion

        #region public methods

        public MapNode GetMapNode(MapType type, string mapName)
        {
            MapNode mapNode = GetNode(mapName, string.Empty, type);
            if(mapNode==null)
                mapNode = new MapNode();

            return mapNode;
        }

        public IList<MapNode> GetChildNodes(MapType type, string mapName)
        {
            return GetNodes(type).Values.Where(node => node.ParentName == mapName).ToList();
        }
        public IList<MapNode> GetChildNodes(MapType type, string mapName, Func<MapNode, bool> func)
        {
            return GetNodes(type).Values.Where(info => mapName == info.ParentName && func(info)).ToList();
        }
        
        /// <summary>
        /// 取得当前Url的mapNode
        /// </summary>
        /// <returns></returns>
        private MapNode CurrentUrlNode(MapType type)
        {
            string url = SiteContext.Current.CurrentUrl;
            return GetNode(string.Empty,url,type);
        }
        public MapNode CurrentUrlNode()
        {
            foreach (MapType type in Enum.GetValues(typeof(MapType)))
            {
                MapNode node = CurrentUrlNode(type);
                if (node != null)
                    return node;
            }
            return null;
        }

        public string GetPathText(bool isShowLink, string split, MapType type)
        {
            MapNode node = CurrentUrlNode(type);
            return GetPathText(isShowLink,node, split, type);
        }
        public string GetPathText(bool isShowLink,string mapNodeKey,string split, MapType type)
        {
            MapNode node = GetNode(mapNodeKey, string.Empty, type);
            return GetPathText(isShowLink,node, split, type);
        }
        private string GetPathText(bool isShowLink, MapNode mapNode, string split, MapType type)
        {
            StringBuilder str = new StringBuilder();
            if (mapNode == null || string.IsNullOrEmpty(mapNode.Path))
                return str.ToString();

            Regex regex = new Regex("^(" + mapNode.Path.Replace("/","|") + ")$");
            IList<MapNode> list = GetNodes(type).Values.Where(node => regex.IsMatch(node.Name)).ToList();
            
            for(int i=0;i<list.Count;i++)
            {
                if (i > 0)
                    str.Append(split);

                string text=list[i].Text;
                string url = list[i].Url;

                str.Append((isShowLink && url.IndexOf("{0}") == -1) ? string.Format("<a href=\"{0}\">{1}</a>", url, text) : text);
            }

            return str.ToString();
        }

        #endregion

        #region private Methods

        private IDictionary<string,MapNode> GetNodes(MapType type)
        {
            return ResourceManagement.Content.Maps[type];
        }

        /// <summary>
        /// 注意，只进行path对比，无法判断参数。
        /// </summary>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="mapType"></param>
        /// <returns></returns>
        private MapNode GetNode(string name, string url, MapType mapType)
        {
            if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(url))
                return null;

            if (!string.IsNullOrEmpty(name))
                return GetNodes(mapType).Values.FirstOrDefault(node => node.Name == name);

            return GetNodes(mapType).Values.FirstOrDefault(info => info.IsMatch(url));
            //if (result != null)
            //    return result;

            //ReWrittenUrl reWrittenUrl = SiteUrls.GetRewriteUrl(url);
            //if (reWrittenUrl != null)
            //{
            //    string pathUrl = UrlHelper.RemoveParameters(SiteUrls.Urls.ResourceUrls[reWrittenUrl.Name].Path).ToLower();
            //    result = GetNodes(mapType).Values.FirstOrDefault(info => !string.IsNullOrEmpty(info.Url) && UrlHelper.RemoveParameters(info.Url).ToLower() == pathUrl);
            //}

            //return result;
        }

        #endregion
    }
}
